<?php

namespace app\controllers;
use yii\web\Controller;
use Yii;
use app\models\content;


class ContentController extends Controller{
    public $enableCsrfValidation = false;
	//信息发布
	public function actionGet(){
		$model = new content();
		if(Yii::$app->request->isPost){
			$postData = Yii::$app->request->bodyParams;
			$first = $_POST['first'];
            $last = $_POST['last'];
            $len = $last - $first;
            $sql = "select * from catch_content where  State = 1  order by Content_ID desc limit $first,$len";
            $data = content::findBysql($sql)->asArray()->all();
	    	echo json_encode(array('error' => 0, 'message' => 'success', 'data' => $data,));
        }    
	}

	//信息接收&数据库输入
	public function actionPublish(){
		$model = new content();
        if( Yii::$app->request->isPost ){
         	$postData = Yii::$app->request->post();              
        	$result = $model->add($postData);
        	if( $result == 0 ){
        		$data = array('Content' => '提交数据成功');
        		echo json_encode(array('error' => 0, 'message' => 'success', 'data' => $data));
        	}else{
        		$data = array('content' => '提交数据失败,请@留言,联系公众号~', 'Content' => $result);
             	echo json_encode(array('error' => 1, 'message' =>fail, 'data' => $data));
        	}
        }
    }

    public function actionLike(){
        $model = new content();
        if( Yii::$app->request->isPost ){
            $postData = Yii::$app->request->post();
            $Content_ID = $postData['id'];
            $LikeCount = $postData['dzSum'];
            $c_arr = $model->find()->select('*')->where('State = :State', [":State"=>'1'])->andwhere('Content_ID = :Content_ID', [":Content_ID"=>$Content_ID])->asArray()->all();
            if(empty($c_arr)){
                $data = array('Content' => '点赞失败');
                echo json_encode(array('error' => -1, 'message' => fail, 'data' => $data));
            }else{
                $modcontent = array('LikeCount' => $LikeCount);
                $model->mod($Content_ID, $modcontent);
                $data = array('Content' => '点赞成功', 'Count_P' => $LikeCount);
                echo json_encode(array('error' => 0, 'message' => 'success', 'data' => $data));
            }
        }
    }        



	public function actionRedischeck($IP){
		$redis = Yii::$app->redis->get($IP);//获取键为$IP的值
        	if($redis){
        		$difftime  = time() - $redis;//对比时间差
        		if( $difftime > 7200 ){
        			Yii::$app->redis->set($IP, time());//更新$IP键值
        			$tip = 0;
        			return $tip;
        		}else{
        			$tip = -1;
        			return $tip;
        		} 
        	}else{
        		Yii::$app->redis->set($IP, time());//设置$IP键值
        		$tip = 1;
        		return $tip;
        	}
	}


}

